package week02;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class AuthenticatorFactory {

    private static Properties prop = new Properties();
    //加载配置文件
    static {
        try {
            prop.load(new FileInputStream("src/week02/auth.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static Authenticator createAuthenticator() {
        //TODO 验证器工厂方法
        return new Authenticator(AuthenticatorFactory.createKeyValueKeeper(),AuthenticatorFactory.createEncrypter());
    }

    private static KeyValueKeeper createKeyValueKeeper() {
        //TODO 键值保存工厂方法
        KeyValueKeeper kvp = null;
        try {
            kvp = (KeyValueKeeper) Class.forName(prop.getProperty("KeyValueKeeper")).newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return kvp;
    }

    private static Encrypter createEncrypter() {
        //TODO 加密器工厂
        Encrypter encrypter = null;
        try {
            encrypter = (Encrypter) Class.forName(prop.getProperty("Encrypter")).newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return encrypter;
    }
}
